from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import *
import re,redis,random
import string
from captcha.image import ImageCaptcha
from django.shortcuts import HttpResponse
# Create your views here.
class Register(APIView):
    def post(self,request):
        name = request.data.get('name')
        password = request.data.get('password')
        mobile = request.data.get('mobile')
        if UserMobile.objects.filter(name=name).count()>0:
            return Response({'code':401,'msg':'用户已存在'})
        UserMobile.objects.create(
            name=name,
            password=password,
            mobile=mobile
        )
        return Response({'code':200,'msg':'注册成功'})

class Login(APIView):
    def post(self,request):
        name = request.data.get('name')
        password = request.data.get('password')
        try:
            user = UserMobile.objects.get(name=name)
        except:
            return Response({'code':401,'msg':'用户已存在'})
        if user.password != password:
            return Response({'code':401,'msg':'密码错误'})
        else:
            return Response({'code':200,'msg':'登入成功'})

class Mobile(APIView):
    def get(self,request):
        mobile = request.data.get('mobile')
        if UserMobile.objects.filter('^[3-9]/d{9}$',mobile):
            return Response({'code':401,'msg':'手机号错误'})
        user_mobile = UserMobile.objects.filter(mobile=mobile)
        if user_mobile:
            return Response({'code':401,'msg':'手机号已存在'})
        return Response({'code':200,'msg':'手机号校验成功'})

class ImageCodeView(APIView):
    def get(self,request,uuid):
        str_num = ''.join(random.sample((string.ascii_letters + string.digits),4))
        img_captcha = ImageCaptcha()
        img = img_captcha.generate(str_num)
        cli = redis.Redis(host='127.0.0.1',port=6379,db=2)
        cli.set('img_id_%s' % uuid,str_num,ex=300)
        return HttpResponse(img,content_type='image/png')

class TraveView(APIView):
    def get(self,request):
        travel_list = TravelMobile.objects.all()
        travel_info = ({
            'id':i.id,
            'line':i.line,
            'circuit':i.circuit,
            'price':i.price
        } for i in travel_list)
        return Response({'code':200,'msg':'获取成功','data':travel_info})

class TraveAddView(APIView):
    def post(self,request):
        line = request.data.get('line')
        circuit = request.data.get('circuit')
        price = request.data.get('price')
        line_info = TravelMobile.objects.filter(line=line).first()
        if line_info:
            return Response({'code':400,'msg':'路线已存在'})
        TravelMobile.objects.create(
            line=line,
            circuit=circuit,
            price=price,
        )
        return Response({'code':200,'msg':'添加成功'})

class TraveDelView(APIView):
    def delete(self,request,id):
        TravelMobile.objects.filter(id=id).delete()
        return Response({'code':200,'msg':'删除成功'})

class TravelUpdView(APIView):
    def put(self,request,id):
        line = request.data.get('line')
        circuit = request.data.get('circuit')
        price = request.data.get('price')
        TravelModel.objects.filter(id=id).update(
            line=line,
            circuit=circuit,
            price=price
        )
        return Response({
            'code': 200,
            'msg': '修改成功'
        })